<template>
  <div class="news-create">
    <h2 class="new-title">发布新闻</h2>
    <el-form ref="newsRef" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="新闻标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="发布时间" prop="time">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.time"
            style="width: 100%"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="特殊资源" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio label="1">热门</el-radio>
          <el-radio label="2">新闻</el-radio>
          <el-radio label="3">公告</el-radio>
          <el-radio label="4">活动</el-radio>
          <el-radio label="5">赛事</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="新闻内容" prop="content">
        <el-input type="textarea" v-model="form.content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="HandleAddNews">立即发布</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addNews } from '@/api/news'
export default {
  data() {
    return {
      form: {
        title: '',
        time: '',
        content: '',
        type: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        type: [{ required: true, message: '请选择种类', trigger: 'change' }],
        content: [
          { required: true, message: '请填写新闻内容', trigger: 'blur' },
          {
            min: 10,
            message: '长度至少10个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    HandleAddNews() {
      this.$refs.newsRef.validate((valid) => {
        if (valid) {
          addNews(this.form)
            .then(() => {})
            .catch(() => {})
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
.news-create {
  .new-title {
    margin-top: 10px;
    margin-bottom: 40px;
    text-indent: 20px;
  }
  .el-form {
    width: 460px;
  }
}
</style>